<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"tallgy.gitee.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="过渡和动画进入&#x2F;离开和列表转换​        Vue提供了多种方法在DOM中的过渡效果  自动为CSS过渡和动画应用类 集成第三方CSS动画库，如 Animate.css 在过渡钩子期间使用 JavaScript 直接操作 DOM 集成第三方JavaScript动画库，例如 Velocity.js  过渡单个元素&#x2F;组件​        Vue 提供了一个transition包装组件，允许您在以下">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue-使用-过渡和动画">
<meta property="og:url" content="http://tallgy.gitee.io/2021/10/29/Vue/Vue2-%E6%95%99%E7%A8%8B/Vue2-%E6%95%99%E7%A8%8B-%E8%BF%87%E6%B8%A1%E5%92%8C%E5%8A%A8%E7%94%BB/index.html">
<meta property="og:site_name" content="tallgy&#39;s blog">
<meta property="og:description" content="过渡和动画进入&#x2F;离开和列表转换​        Vue提供了多种方法在DOM中的过渡效果  自动为CSS过渡和动画应用类 集成第三方CSS动画库，如 Animate.css 在过渡钩子期间使用 JavaScript 直接操作 DOM 集成第三方JavaScript动画库，例如 Velocity.js  过渡单个元素&#x2F;组件​        Vue 提供了一个transition包装组件，允许您在以下">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://tallgy.gitee.io/.io//Vue2-%E6%95%99%E7%A8%8B-%E8%BF%87%E6%B8%A1%E5%92%8C%E5%8A%A8%E7%94%BB%5Cimage-20211203152116522.png">
<meta property="og:image" content="http://tallgy.gitee.io/.io//image-20211203154333481.png">
<meta property="article:published_time" content="2021-10-29T15:15:25.000Z">
<meta property="article:modified_time" content="2023-06-25T08:57:29.323Z">
<meta property="article:author" content="tallgy">
<meta property="article:tag" content="Vue">
<meta property="article:tag" content="文档">
<meta property="article:tag" content="Vue2">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://tallgy.gitee.io/.io//Vue2-%E6%95%99%E7%A8%8B-%E8%BF%87%E6%B8%A1%E5%92%8C%E5%8A%A8%E7%94%BB%5Cimage-20211203152116522.png">


<link rel="canonical" href="http://tallgy.gitee.io/2021/10/29/Vue/Vue2-%E6%95%99%E7%A8%8B/Vue2-%E6%95%99%E7%A8%8B-%E8%BF%87%E6%B8%A1%E5%92%8C%E5%8A%A8%E7%94%BB/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://tallgy.gitee.io/2021/10/29/Vue/Vue2-%E6%95%99%E7%A8%8B/Vue2-%E6%95%99%E7%A8%8B-%E8%BF%87%E6%B8%A1%E5%92%8C%E5%8A%A8%E7%94%BB/","path":"2021/10/29/Vue/Vue2-教程/Vue2-教程-过渡和动画/","title":"Vue-使用-过渡和动画"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Vue-使用-过渡和动画 | tallgy's blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">tallgy's blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E8%BF%87%E6%B8%A1%E5%92%8C%E5%8A%A8%E7%94%BB"><span class="nav-number">1.</span> <span class="nav-text">过渡和动画</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%BF%9B%E5%85%A5-%E7%A6%BB%E5%BC%80%E5%92%8C%E5%88%97%E8%A1%A8%E8%BD%AC%E6%8D%A2"><span class="nav-number">1.1.</span> <span class="nav-text">进入&#x2F;离开和列表转换</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BF%87%E6%B8%A1%E5%8D%95%E4%B8%AA%E5%85%83%E7%B4%A0-%E7%BB%84%E4%BB%B6"><span class="nav-number">1.1.1.</span> <span class="nav-text">过渡单个元素&#x2F;组件</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%BF%87%E6%B8%A1%E7%9A%84%E7%B1%BB%E5%90%8D"><span class="nav-number">1.1.1.1.</span> <span class="nav-text">过渡的类名</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#CSS%E8%BF%87%E6%B8%A1"><span class="nav-number">1.1.1.2.</span> <span class="nav-text">CSS过渡</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E8%BF%87%E6%B8%A1%E7%9A%84%E7%B1%BB%E5%90%8D"><span class="nav-number">1.1.1.3.</span> <span class="nav-text">自定义过渡的类名</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%90%8C%E6%97%B6%E4%BD%BF%E7%94%A8%E8%BF%87%E6%B8%A1%E5%92%8C%E5%8A%A8%E7%94%BB"><span class="nav-number">1.1.1.4.</span> <span class="nav-text">同时使用过渡和动画</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%98%BE%E6%80%A7%E7%9A%84%E8%BF%87%E6%B8%A1%E6%8C%81%E7%BB%AD%E6%97%B6%E9%97%B4"><span class="nav-number">1.1.1.5.</span> <span class="nav-text">显性的过渡持续时间</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#JavaScript-%E9%92%A9%E5%AD%90"><span class="nav-number">1.1.1.6.</span> <span class="nav-text">JavaScript 钩子</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%9D%E5%A7%8B%E6%B8%B2%E6%9F%93%E7%9A%84%E8%BF%87%E6%B8%A1"><span class="nav-number">1.1.2.</span> <span class="nav-text">初始渲染的过渡</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%9A%E4%B8%AA%E5%85%83%E7%B4%A0%E7%9A%84%E8%BF%87%E6%B8%A1"><span class="nav-number">1.1.3.</span> <span class="nav-text">多个元素的过渡</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%BF%87%E6%B8%A1%E6%A8%A1%E5%BC%8F"><span class="nav-number">1.1.3.1.</span> <span class="nav-text">过渡模式</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%9A%E7%BB%84%E4%BB%B6%E7%9A%84%E8%BF%87%E6%B8%A1"><span class="nav-number">1.1.4.</span> <span class="nav-text">多组件的过渡</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%97%E8%A1%A8%E8%BF%87%E6%B8%A1"><span class="nav-number">1.1.5.</span> <span class="nav-text">列表过渡</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%88%97%E8%A1%A8%E7%9A%84%E8%BF%9B%E5%85%A5-%E7%A6%BB%E5%BC%80%E8%BF%87%E6%B8%A1"><span class="nav-number">1.1.5.1.</span> <span class="nav-text">列表的进入&#x2F;离开过渡</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%8E%92%E5%BA%8F%E7%9A%84%E8%BF%87%E6%B8%A1"><span class="nav-number">1.1.5.2.</span> <span class="nav-text">排序的过渡</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BA%A4%E9%94%99%E7%9A%84%E8%BF%87%E6%B8%A1"><span class="nav-number">1.1.5.3.</span> <span class="nav-text">交错的过渡</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%AF%E5%A4%8D%E7%94%A8%E7%9A%84%E8%BF%87%E6%B8%A1"><span class="nav-number">1.1.6.</span> <span class="nav-text">可复用的过渡</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8A%A8%E6%80%81%E8%BF%87%E6%B8%A1"><span class="nav-number">1.1.7.</span> <span class="nav-text">动态过渡</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%8A%B6%E6%80%81%E8%BF%87%E6%B8%A1"><span class="nav-number">1.2.</span> <span class="nav-text">状态过渡</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%8A%B6%E6%80%81%E5%8A%A8%E7%94%BB%E4%B8%8E%E4%BE%A6%E5%90%AC%E5%99%A8"><span class="nav-number">1.2.1.</span> <span class="nav-text">状态动画与侦听器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8A%A8%E6%80%81%E7%8A%B6%E6%80%81%E8%BF%87%E6%B8%A1"><span class="nav-number">1.2.2.</span> <span class="nav-text">动态状态过渡</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8A%8A%E8%BF%87%E6%B8%A1%E6%94%BE%E5%88%B0%E7%BB%84%E4%BB%B6%E9%87%8C"><span class="nav-number">1.2.3.</span> <span class="nav-text">把过渡放到组件里</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%B5%8B%E4%BA%88%E8%AE%BE%E8%AE%A1%E4%BB%A5%E7%94%9F%E5%91%BD"><span class="nav-number">1.2.4.</span> <span class="nav-text">赋予设计以生命</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">tallgy</p>
  <div class="site-description" itemprop="description">只是一个知识的搬运工</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">107</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">74</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2021/10/29/Vue/Vue2-%E6%95%99%E7%A8%8B/Vue2-%E6%95%99%E7%A8%8B-%E8%BF%87%E6%B8%A1%E5%92%8C%E5%8A%A8%E7%94%BB/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Vue-使用-过渡和动画
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-10-29 23:15:25" itemprop="dateCreated datePublished" datetime="2021-10-29T23:15:25+08:00">2021-10-29</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2023-06-25 16:57:29" itemprop="dateModified" datetime="2023-06-25T16:57:29+08:00">2023-06-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Vue/" itemprop="url" rel="index"><span itemprop="name">Vue</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Vue/Vue2%E6%96%87%E6%A1%A3/" itemprop="url" rel="index"><span itemprop="name">Vue2文档</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="过渡和动画"><a href="#过渡和动画" class="headerlink" title="过渡和动画"></a>过渡和动画</h1><h2 id="进入-离开和列表转换"><a href="#进入-离开和列表转换" class="headerlink" title="进入/离开和列表转换"></a>进入/离开和列表转换</h2><p>​        Vue提供了多种方法在DOM中的过渡效果</p>
<ul>
<li>自动为CSS过渡和动画应用类</li>
<li>集成第三方CSS动画库，如 Animate.css</li>
<li>在过渡钩子期间使用 JavaScript 直接操作 DOM</li>
<li>集成第三方JavaScript动画库，例如 Velocity.js</li>
</ul>
<h3 id="过渡单个元素-组件"><a href="#过渡单个元素-组件" class="headerlink" title="过渡单个元素/组件"></a>过渡单个元素/组件</h3><p>​        Vue 提供了一个<code>transition</code>包装组件，允许您在以下上下文中为任何元素或组件添加进入/离开转换：</p>
<ul>
<li>条件渲染（使用<code>v-if</code>）</li>
<li>条件显示（使用<code>v-show</code>）</li>
<li>动态组件</li>
<li>组件根节点</li>
</ul>
<p>​        在这里我直接复制官网的例子，并加以一个简单的说明，首先，对于transition来说，需要的是一个根组件（根元素），对于存在多个根组件的，便会只渲染一个，其次，对于不是非根元素进行的渲染显示不会有效果。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">&lt;div id=&quot;demo&quot;&gt;</span><br><span class="line">  &lt;button v-on:click=&quot;show = !show&quot;&gt;</span><br><span class="line">    Toggle</span><br><span class="line">  &lt;/button&gt;</span><br><span class="line">  &lt;transition name=&quot;fade&quot;&gt;</span><br><span class="line">    &lt;p v-if=&quot;show&quot;&gt;hello&lt;/p&gt;</span><br><span class="line">  &lt;/transition&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line"></span><br><span class="line">new Vue(&#123;</span><br><span class="line">  el: &#x27;#demo&#x27;,</span><br><span class="line">  data: &#123;</span><br><span class="line">    show: true</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">.fade-enter-active, .fade-leave-active &#123;</span><br><span class="line">  transition: opacity .5s;</span><br><span class="line">&#125;</span><br><span class="line">.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ &#123;</span><br><span class="line">  opacity: 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>当<code>transition</code>插入或删除包裹在组件中的元素时，会发生以下情况：</p>
<ol>
<li>Vue 会自动嗅探目标元素是否应用了 CSS 过渡或动画。如果是这样，CSS 过渡类将在适当的时间添加/删除。</li>
<li>如果转换组件提供了<a target="_blank" rel="noopener" href="https://vuejs.org/v2/guide/transitions.html#JavaScript-Hooks">JavaScript hooks</a>，这些 hooks 将在适当的时间被调用。</li>
<li>如果没有检测到 CSS 过渡/动画，也没有提供 JavaScript 钩子，插入和/或移除的 DOM 操作将在下一帧立即执行（注意：这是一个浏览器动画帧，与 Vue 的概念不同<code>nextTick</code>）。</li>
</ol>
<h4 id="过渡的类名"><a href="#过渡的类名" class="headerlink" title="过渡的类名"></a>过渡的类名</h4><p>​        在进入/离开的过程中，会有6个 class 切换</p>
<ul>
<li>v-enter<ul>
<li>进入的起始状态，在插入元素之前添加，在插入元素后移除一帧</li>
</ul>
</li>
<li>v-enter-active<ul>
<li>进入的活动状态，在整个进入阶段应用。插入元素之前添加，在过渡完成时移除。定义进入过渡的事件、延迟和缓动曲线。</li>
</ul>
</li>
<li>v-enter-to<ul>
<li>2.1.8+</li>
<li>进入的结束状态。插入元素后添加一帧（同时 v-enter 删除），在过渡完成时删除。</li>
</ul>
</li>
<li>v-leave<ul>
<li>定义离开过渡的开始状态。在离开过渡被触发时立刻生效。下一帧被移除</li>
</ul>
</li>
<li>v-leave-active<ul>
<li>定义离开过渡生效时的状态。在离开过渡立刻生效，过渡完成被移除，同 v-enter-active</li>
</ul>
</li>
<li>v-leave-to<ul>
<li>2.1.8 +</li>
<li>定义离开过渡的结束状态。离开过渡被触发后下一帧生效（同时 v-leave 被删除）。过渡完成被移除</li>
</ul>
</li>
</ul>
<p><img src="/.io//Vue2-%E6%95%99%E7%A8%8B-%E8%BF%87%E6%B8%A1%E5%92%8C%E5%8A%A8%E7%94%BB%5Cimage-20211203152116522.png" alt="image-20211203152116522"></p>
<p>​        在这些过渡的切换中，如果你使用一个没有名字的transition，则v-是这些类名的默认前缀。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;transition&gt;</span><br><span class="line">    &lt;p v-show=&quot;show&quot;&gt;hello&lt;/p&gt;</span><br><span class="line">&lt;/transition&gt;</span><br><span class="line"></span><br><span class="line">.v-enter-active,</span><br></pre></td></tr></table></figure>

<p>​        如果你使用了 <code>&lt;transition name=&quot;my-transition&quot;&gt;</code> ，那么 <code>v-enter</code> 会替换为 <code>my-transition-enter</code> 。</p>
<p>​        v-enter-active 和 v-leave-active 可以控制进入/离开过渡的不同的缓和曲线。</p>
<img src="/.io//image-20211203154333481.png" alt="image-20211203154333481">

<p>​        通过图示可以看出，当元素进行显示时：</p>
<ul>
<li>先插入 v-enter 和 v-enter-active， </li>
<li>然后再下一帧删除 v-enter，出现了 v-enter-to</li>
<li>然后结束了 v-enter-active 和 v-enter-to 消失。</li>
</ul>
<p>v-leave 是一样的效果。</p>
<h4 id="CSS过渡"><a href="#CSS过渡" class="headerlink" title="CSS过渡"></a>CSS过渡</h4><p>​        和普通的过渡是没有什么区别的。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">.bounce-enter-active &#123;</span><br><span class="line">  animation: bounce-in .5s;</span><br><span class="line">&#125;</span><br><span class="line">.bounce-leave-active &#123;</span><br><span class="line">  animation: bounce-in .5s reverse;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">@keyframes bounce-in &#123;</span><br><span class="line">  0% &#123;</span><br><span class="line">    transform: scale(0);</span><br><span class="line">  &#125;</span><br><span class="line">  50% &#123;</span><br><span class="line">    transform: scale(1.5);</span><br><span class="line">  &#125;</span><br><span class="line">  100% &#123;</span><br><span class="line">    transform: scale(1);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h4 id="自定义过渡的类名"><a href="#自定义过渡的类名" class="headerlink" title="自定义过渡的类名"></a>自定义过渡的类名</h4><p>​        我们可以通过使用以下的属性来自定义过渡的类名。并且他们的优先级会高于普通的类名。因此对于使用其他第三方css库十分的有用。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">enter-class</span><br><span class="line">enter-active-class</span><br><span class="line">enter-to-class (2.1.8+)</span><br><span class="line">leave-class</span><br><span class="line">leave-active-class</span><br><span class="line">leave-to-class (2.1.8+)</span><br></pre></td></tr></table></figure>

<p>​        这里name就是往常一样的name，然后在元素里面添加了标签，标签是 enter-xx-class，或者是 leave-xx-class 这种。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;transition</span><br><span class="line">name=&quot;custom-classes-transition&quot;</span><br><span class="line">enter-active-class=&quot;animated tada&quot;</span><br><span class="line">leave-active-class=&quot;animated bounceOutRight&quot;</span><br><span class="line">&gt;</span><br><span class="line">    &lt;p v-if=&quot;show&quot;&gt;hello&lt;/p&gt;</span><br><span class="line">&lt;/transition&gt;</span><br></pre></td></tr></table></figure>



<h4 id="同时使用过渡和动画"><a href="#同时使用过渡和动画" class="headerlink" title="同时使用过渡和动画"></a>同时使用过渡和动画</h4><p>​        Vue为了能知道过渡的完成，必须设置相应的事件监听器。他可以是 transitionend 或者是 animationend，如果你只是使用了其中一种，Vue可以自动识别并设置监听。</p>
<p>​        但是如果你同时使用了 transition 和 animation，那么这种情况你就需要使用 type 属性来指定Vue需要监听的类型。</p>
<p>​        如果你不指定的话，我从使用上看来他会默认监听的是时间最长的那个，就是延迟时间和运行时间加起来最大的那个。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;transition name=&quot;slide-fade&quot; type=&quot;transition&quot;&gt;</span><br><span class="line">    &lt;p v-if=&quot;show&quot;&gt; content &lt;/p&gt;</span><br><span class="line">&lt;/transition&gt;</span><br></pre></td></tr></table></figure>



<h4 id="显性的过渡持续时间"><a href="#显性的过渡持续时间" class="headerlink" title="显性的过渡持续时间"></a>显性的过渡持续时间</h4><p>​        2.2.0 新增</p>
<p>​        默认情况下，Vue会自动计算出过渡时间。会等待过渡效果的 transitionend 和 animationend 事件。</p>
<p>​        但是，我们可以显性的设置一个过渡的持续时间。</p>
<p>​        下面这个代码的意思就是，对于使用了 duration 设置了显性的过渡时间了的，在到达过渡的时间之后，便会直接进行过渡的结束。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;transition :duration=&quot;1000&quot;&gt;...&lt;/transition&gt;</span><br><span class="line"></span><br><span class="line">&lt;transition :duration=&quot;&#123; enter: 500, leave: 800 &#125;&quot;&gt;...&lt;/transition&gt;</span><br></pre></td></tr></table></figure>



<h4 id="JavaScript-钩子"><a href="#JavaScript-钩子" class="headerlink" title="JavaScript 钩子"></a>JavaScript 钩子</h4><p>​        可以在属性中声明 JavaScript 钩子。</p>
<p>​        这里我简单介绍一下 JavaScript钩子的执行时机，虽然通过名字也能够大致猜的出来意思</p>
<ul>
<li>before-enter： 简单来说就是在enter启动之前，就像是前面的 v-enter</li>
<li>enter： 就是在调用期间， v-enter-to，但是这个 JavaScript钩子 没有说版本要求，而 v-enter-to 是2.1.8以上的，所以这个应该是更像 v-enter-active 。</li>
<li>after-enter： 就是在就是结束时会进行的调用，应该接近于 animationend 或者 transitionend 这两个回调。</li>
<li>enter-cancalled： 就是在没有调用after-enter的时候就又进行了一次切换，此时就会调用 enter-cancalled 回调。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;transition</span><br><span class="line">  v-on:before-enter=&quot;beforeEnter&quot;</span><br><span class="line">  v-on:enter=&quot;enter&quot;</span><br><span class="line">  v-on:after-enter=&quot;afterEnter&quot;</span><br><span class="line">  v-on:enter-cancelled=&quot;enterCancelled&quot;</span><br><span class="line"></span><br><span class="line">  v-on:before-leave=&quot;beforeLeave&quot;</span><br><span class="line">  v-on:leave=&quot;leave&quot;</span><br><span class="line">  v-on:after-leave=&quot;afterLeave&quot;</span><br><span class="line">  v-on:leave-cancelled=&quot;leaveCancelled&quot;</span><br><span class="line">&gt;</span><br><span class="line">  &lt;!-- ... --&gt;</span><br><span class="line">&lt;/transition&gt;</span><br></pre></td></tr></table></figure>

<p>​        这里需要注意，</p>
<ul>
<li>当只用 JavaScript 过渡的时候，<strong>在 <code>enter</code> 和 <code>leave</code> 中必须使用 <code>done</code> 进行回调</strong>。否则，它们将被同步调用，过渡会立即完成。<ul>
<li>这个我没有仔细使用过函数进行动画的过渡，而是进行一些其他的操作，但是这里 done的回调是必须的。因为我们可以看到如果不使用done函数，那么便不会调用 after-enter 来结束，所以这个done函数更像是 生成器函数的 next 一样。</li>
<li>这里我使用了css进行过渡，然后JavaScript进行钩子的调用，发现css的过渡没有出现效果，直接只能使用钩子的调用了。就算是使用自定义过渡类名属性 enter-class 这些也没有效果。</li>
</ul>
</li>
<li>推荐对于仅使用 JavaScript 过渡的元素添加 <code>v-bind:css=&quot;false&quot;</code>，Vue 会跳过 CSS 的检测。这也可以避免过渡过程中 CSS 的影响。</li>
<li>leaveCancelled 只能用于 v-show 中。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">// ...</span><br><span class="line">methods: &#123;</span><br><span class="line">  // --------</span><br><span class="line">  // 进入中</span><br><span class="line">  // --------</span><br><span class="line"></span><br><span class="line">  beforeEnter: function (el) &#123;</span><br><span class="line">    // ...</span><br><span class="line">  &#125;,</span><br><span class="line">  // 当与 CSS 结合使用时</span><br><span class="line">  // 回调函数 done 是可选的</span><br><span class="line">  enter: function (el, done) &#123;</span><br><span class="line">    // ...</span><br><span class="line">    done()</span><br><span class="line">  &#125;,</span><br><span class="line">  afterEnter: function (el) &#123;</span><br><span class="line">    // ...</span><br><span class="line">  &#125;,</span><br><span class="line">  enterCancelled: function (el) &#123;</span><br><span class="line">    // ...</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  // --------</span><br><span class="line">  // 离开时</span><br><span class="line">  // --------</span><br><span class="line"></span><br><span class="line">  beforeLeave: function (el) &#123;</span><br><span class="line">    // ...</span><br><span class="line">  &#125;,</span><br><span class="line">  // 当与 CSS 结合使用时</span><br><span class="line">  // 回调函数 done 是可选的</span><br><span class="line">  leave: function (el, done) &#123;</span><br><span class="line">    // ...</span><br><span class="line">    done()</span><br><span class="line">  &#125;,</span><br><span class="line">  afterLeave: function (el) &#123;</span><br><span class="line">    // ...</span><br><span class="line">  &#125;,</span><br><span class="line">  // leaveCancelled 只用于 v-show 中</span><br><span class="line">  leaveCancelled: function (el) &#123;</span><br><span class="line">    // ...</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="初始渲染的过渡"><a href="#初始渲染的过渡" class="headerlink" title="初始渲染的过渡"></a>初始渲染的过渡</h3><p>​        通过使用 appear 属性设置节点初始渲染的过渡</p>
<p>​        简单来说就是会在开始的时候有一个过渡的效果。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;transition appear&gt;</span><br></pre></td></tr></table></figure>

<p>​    可以自定义 css 类名</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;transition</span><br><span class="line">  appear</span><br><span class="line">  appear-class=&quot;custom-appear-class&quot;</span><br><span class="line">  appear-to-class=&quot;custom-appear-to-class&quot; (2.1.8+)</span><br><span class="line">  appear-active-class=&quot;custom-appear-active-class&quot;</span><br><span class="line">&gt;</span><br></pre></td></tr></table></figure>

<p>​    自定义JavaScript 钩子</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;transition</span><br><span class="line">  appear</span><br><span class="line">  v-on:before-appear=&quot;customBeforeAppearHook&quot;</span><br><span class="line">  v-on:appear=&quot;customAppearHook&quot;</span><br><span class="line">  v-on:after-appear=&quot;customAfterAppearHook&quot;</span><br><span class="line">  v-on:appear-cancelled=&quot;customAppearCancelledHook&quot;</span><br><span class="line">&gt;</span><br></pre></td></tr></table></figure>

<p>​        在上面的例子中，无论是使用 appear attribute 还是使用 v-on:appear 钩子都会生成初始渲染的过渡。</p>
<h3 id="多个元素的过渡"><a href="#多个元素的过渡" class="headerlink" title="多个元素的过渡"></a>多个元素的过渡</h3><p>​        对于原生的标签，可以使用 v-if / v-else 进行过渡。</p>
<blockquote>
<p>当有<strong>相同标签名</strong>的元素切换时，需要通过 <code>key</code> attribute 设置唯一的值来标记以让 Vue 区分它们，否则 Vue 为了效率只会替换相同标签内部的内容。即使在技术上没有必要，<strong>给在 <code>&lt;transition&gt;</code> 组件中的多个元素设置 key 是一个更好的实践。</strong></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;transition&gt;</span><br><span class="line">  &lt;button v-if=&quot;isEditing&quot; key=&quot;save&quot;&gt;</span><br><span class="line">    Save</span><br><span class="line">  &lt;/button&gt;</span><br><span class="line">  &lt;button v-else key=&quot;edit&quot;&gt;</span><br><span class="line">    Edit</span><br><span class="line">  &lt;/button&gt;</span><br><span class="line">&lt;/transition&gt;</span><br></pre></td></tr></table></figure>

<p>​        在某些场景，也可以通过给同一个元素的key属性设置不同的状态来代替 v-if 和 v-else。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;transition&gt;</span><br><span class="line">  &lt;button v-bind:key=&quot;isEditing&quot;&gt;</span><br><span class="line">    &#123;&#123; isEditing ? &#x27;Save&#x27; : &#x27;Edit&#x27; &#125;&#125;</span><br><span class="line">  &lt;/button&gt;</span><br><span class="line">&lt;/transition&gt;</span><br></pre></td></tr></table></figure>



<h4 id="过渡模式"><a href="#过渡模式" class="headerlink" title="过渡模式"></a>过渡模式</h4><p>​        简单来说就是元素的切换是同步的，所以会出现占位的问题，并且当使用的是 v-if 的时候，开始会占位，但是在过渡结束之后就会出现位置消失，然后后面的元素会补充上去。</p>
<p>​        所以此时我们就需要进行处理。</p>
<p>​        解决方式一般是使用定位的形式，因为定位是脱离了文档流的，所以就不会出现这个问题了。然后此时我们再使用一个 transition 进行过渡的显示便有了效果。</p>
<p>​        当然同时生效的进入和离开的过渡不能满足所有要求，所以Vue提供了<strong>过渡模式</strong> mode</p>
<ul>
<li><code>in-out</code> ：新元素先过渡，完成后旧元素过渡离开。</li>
<li><code>out-in</code> ：旧元素先过渡，完成后新元素过渡进入。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;transition name=&quot;fade&quot; mode=&quot;out-in&quot;&gt;</span><br><span class="line">  &lt;!-- ... the buttons ... --&gt;</span><br><span class="line">&lt;/transition&gt;</span><br></pre></td></tr></table></figure>



<h3 id="多组件的过渡"><a href="#多组件的过渡" class="headerlink" title="多组件的过渡"></a>多组件的过渡</h3><p>​        我们可以直接使用动态组件来进行组件之间的过渡</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;transition name=&quot;component-fade&quot; mode=&quot;out-in&quot;&gt;</span><br><span class="line">  &lt;component v-bind:is=&quot;view&quot;&gt;&lt;/component&gt;</span><br><span class="line">&lt;/transition&gt;</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">new Vue(&#123;</span><br><span class="line">  el: &#x27;#transition-components-demo&#x27;,</span><br><span class="line">  data: &#123;</span><br><span class="line">    view: &#x27;v-a&#x27;</span><br><span class="line">  &#125;,</span><br><span class="line">  components: &#123;</span><br><span class="line">    &#x27;v-a&#x27;: &#123;</span><br><span class="line">      template: &#x27;&lt;div&gt;Component A&lt;/div&gt;&#x27;</span><br><span class="line">    &#125;,</span><br><span class="line">    &#x27;v-b&#x27;: &#123;</span><br><span class="line">      template: &#x27;&lt;div&gt;Component B&lt;/div&gt;&#x27;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>



<h3 id="列表过渡"><a href="#列表过渡" class="headerlink" title="列表过渡"></a>列表过渡</h3><p>​        对于使用v-for渲染时的过渡。可以使用 transition-group 组件。</p>
<p><strong>特点</strong>：</p>
<ul>
<li>transition-group 会以一个真实的元素呈现：默认为 span，也可以通过 tag 属性来更换为其他元素。</li>
<li>不能使用过渡模式。mode</li>
<li>内部元素总是需要提供一个唯一的 key 属性值</li>
<li>CSS 过渡的类 将会应用在内部元素中，而不是这个 组件/容器 本身。</li>
</ul>
<p>​        这里，首先name为list，其次使用了tag，默认不使用tag时为span，使用了tag为p，所以就是p标签进行包裹。然后内部是使用span标签进行的for循环。最后，使用transition-group，key值是必须的。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;transition-group name=&quot;list&quot; tag=&quot;p&quot;&gt;</span><br><span class="line">	&lt;span v-for=&quot;item in items&quot; v-bind:key=&quot;item&quot; class=&quot;list-item&quot;&gt;</span><br><span class="line">		&#123;&#123; item &#125;&#125;</span><br><span class="line">	&lt;/span&gt;</span><br><span class="line">&lt;/transition-group&gt;</span><br></pre></td></tr></table></figure>



<h4 id="列表的进入-离开过渡"><a href="#列表的进入-离开过渡" class="headerlink" title="列表的进入/离开过渡"></a>列表的进入/离开过渡</h4><p>​        列表的进入/离开过渡是和以前的过渡的方式是一样的。只是过渡变成了每一个小组件了。</p>
<p>​        问题就是过渡的平滑展现，比如新增的元素，周围的元素会瞬间移动到他们新布局的位置，而不是平滑的过渡。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">.list-enter-active, .list-leave-active &#123;</span><br><span class="line">  transition: all 1s;</span><br><span class="line">&#125;</span><br><span class="line">.list-enter, .list-leave-to</span><br><span class="line">/* .list-leave-active for below version 2.1.8 */ &#123;</span><br><span class="line">  opacity: 0;</span><br><span class="line">  transform: translateY(30px);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h4 id="排序的过渡"><a href="#排序的过渡" class="headerlink" title="排序的过渡"></a>排序的过渡</h4><p>​        可以通过使用 v-move 类来实现列表的移动。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">&lt;transition-group name=&quot;list&quot; tag=&quot;ul&quot;&gt;</span><br><span class="line">    &lt;li v-for=&quot;(item, index) in items&quot; v-bind:key=&quot;item&quot; class=&quot;list-item&quot;&gt;</span><br><span class="line">		&#123;&#123; item &#125;&#125;</span><br><span class="line">    &lt;/li&gt;</span><br><span class="line">&lt;/transition-group&gt;</span><br><span class="line"></span><br><span class="line">.list-item &#123;</span><br><span class="line">    transition: all 1s;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">.list-enter,</span><br><span class="line">.list-leave-to &#123;</span><br><span class="line">    opacity: 0;</span><br><span class="line">    transform: translateY(30px);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>​        如果我对列表进行一次排序。就会发现动画的效果。</p>
<p>​    这个看起来很神奇，内部的实现，Vue 使用了一个叫 <a target="_blank" rel="noopener" href="https://aerotwist.com/blog/flip-your-animations/">FLIP</a> 简单的动画队列。</p>
<p>​        当然，前面的style样式对于增加排序时的效果都还是不错，但是对于删除来说，效果就不一样了。</p>
<p>​    于是我们对于要进行删除的元素进行了一个绝对定位。</p>
<p>​    对于为什么要使用绝对定位，那是因为当使用了绝对定位之后，那便不会在同一图层。然后此时后面的元素便会向前移动，然后再通过 transition: all .5s; 来进行一个动态的变化。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">.list-leave-active &#123;</span><br><span class="line">    position: absolute;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>需要注意的是使用 FLIP 过渡的元素不能设置为 <code>display: inline</code> 。作为替代方案，可以设置为 <code>display: inline-block</code> 或者放置于 flex 中</p>
</blockquote>
<p><strong>多维网格也可以进行过渡</strong>：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">  &lt;head&gt;</span><br><span class="line">    &lt;title&gt;List Move Transitions Sudoku Example&lt;/title&gt;</span><br><span class="line">    &lt;script src=&quot;https://unpkg.com/vue&quot;&gt;&lt;/script&gt;</span><br><span class="line">    &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;./style.css&quot; /&gt;</span><br><span class="line">  &lt;/head&gt;</span><br><span class="line">  &lt;body&gt;</span><br><span class="line">    &lt;div id=&quot;sudoku-demo&quot; class=&quot;demo&quot;&gt;</span><br><span class="line">      &lt;h1&gt;Lazy Sudoku&lt;/h1&gt;</span><br><span class="line">      &lt;p&gt;Keep hitting the shuffle button until you win.&lt;/p&gt;</span><br><span class="line"></span><br><span class="line">      &lt;button @click=&quot;shuffle&quot;&gt;</span><br><span class="line">        Shuffle</span><br><span class="line">      &lt;/button&gt;</span><br><span class="line">      &lt;transition-group name=&quot;cell&quot; tag=&quot;div&quot; class=&quot;container&quot;&gt;</span><br><span class="line">        &lt;div v-for=&quot;cell in cells&quot; :key=&quot;cell.id&quot; class=&quot;cell&quot;&gt;</span><br><span class="line">          &#123;&#123; cell.number &#125;&#125;</span><br><span class="line">        &lt;/div&gt;</span><br><span class="line">      &lt;/transition-group&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line"></span><br><span class="line">    &lt;script&gt;</span><br><span class="line">      new Vue(&#123;</span><br><span class="line">        el: &quot;#sudoku-demo&quot;,</span><br><span class="line">        data: &#123;</span><br><span class="line">          cells: Array.apply(null, &#123; length: 81 &#125;).map(function(_, index) &#123;</span><br><span class="line">            return &#123;</span><br><span class="line">              id: index,</span><br><span class="line">              number: (index % 9) + 1</span><br><span class="line">            &#125;;</span><br><span class="line">          &#125;)</span><br><span class="line">        &#125;,</span><br><span class="line">        methods: &#123;</span><br><span class="line">          shuffle: function() &#123;</span><br><span class="line">            this.cells = this.randomSort(this.cells);</span><br><span class="line">          &#125;,</span><br><span class="line">          randomSort(arr) &#123;</span><br><span class="line">            const res = arr.slice();</span><br><span class="line">            for (let i=0; i&lt;arr.length; i++) &#123;</span><br><span class="line">              const index = this.randomIndex();</span><br><span class="line">              [res[i], res[index]] = [res[index], res[i]];</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            return res;</span><br><span class="line">          &#125;,</span><br><span class="line">          randomIndex() &#123;</span><br><span class="line">            return Math.floor(Math.random() * this.cells.length);</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;);</span><br><span class="line">    &lt;/script&gt;</span><br><span class="line">  &lt;/body&gt;</span><br><span class="line">  </span><br><span class="line">  &lt;style&gt;</span><br><span class="line">.container &#123;</span><br><span class="line">  display: flex;</span><br><span class="line">  flex-wrap: wrap;</span><br><span class="line">  width: 238px;</span><br><span class="line">  margin-top: 10px;</span><br><span class="line">&#125;</span><br><span class="line">.cell &#123;</span><br><span class="line">  display: flex;</span><br><span class="line">  justify-content: space-around;</span><br><span class="line">  align-items: center;</span><br><span class="line">  width: 25px;</span><br><span class="line">  height: 25px;</span><br><span class="line">  border: 1px solid #aaa;</span><br><span class="line">  margin-right: -1px;</span><br><span class="line">  margin-bottom: -1px;</span><br><span class="line">&#125;</span><br><span class="line">.cell:nth-child(3n) &#123;</span><br><span class="line">  margin-right: 0;</span><br><span class="line">&#125;</span><br><span class="line">.cell:nth-child(27n) &#123;</span><br><span class="line">  margin-bottom: 0;</span><br><span class="line">&#125;</span><br><span class="line">.cell-move &#123;</span><br><span class="line">  transition: transform 1s;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">  &lt;/style&gt;</span><br><span class="line">  </span><br><span class="line">&lt;/html&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h4 id="交错的过渡"><a href="#交错的过渡" class="headerlink" title="交错的过渡"></a>交错的过渡</h4><p>​        通过data属性和JavaScript的通信，可以实现列表的交错过渡。简单来说就是使用js方法进行了渲染。因为 velocity 不能访问，所以没有进行实验。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line">&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/velocity/1.2.3/velocity.min.js&quot;&gt;&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">&lt;div id=&quot;staggered-list-demo&quot;&gt;</span><br><span class="line">  &lt;input v-model=&quot;query&quot;&gt;</span><br><span class="line">  &lt;transition-group</span><br><span class="line">    name=&quot;staggered-fade&quot;</span><br><span class="line">    tag=&quot;ul&quot;</span><br><span class="line">    v-bind:css=&quot;false&quot;</span><br><span class="line">    v-on:before-enter=&quot;beforeEnter&quot;</span><br><span class="line">    v-on:enter=&quot;enter&quot;</span><br><span class="line">    v-on:leave=&quot;leave&quot;</span><br><span class="line">  &gt;</span><br><span class="line">    &lt;li</span><br><span class="line">      v-for=&quot;(item, index) in computedList&quot;</span><br><span class="line">      v-bind:key=&quot;item.msg&quot;</span><br><span class="line">      v-bind:data-index=&quot;index&quot;</span><br><span class="line">    &gt;&#123;&#123; item.msg &#125;&#125;&lt;/li&gt;</span><br><span class="line">  &lt;/transition-group&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line"></span><br><span class="line">new Vue(&#123;</span><br><span class="line">  el: &#x27;#staggered-list-demo&#x27;,</span><br><span class="line">  data: &#123;</span><br><span class="line">    query: &#x27;&#x27;,</span><br><span class="line">    list: [</span><br><span class="line">      &#123; msg: &#x27;Bruce Lee&#x27; &#125;,</span><br><span class="line">      &#123; msg: &#x27;Jackie Chan&#x27; &#125;,</span><br><span class="line">      &#123; msg: &#x27;Chuck Norris&#x27; &#125;,</span><br><span class="line">      &#123; msg: &#x27;Jet Li&#x27; &#125;,</span><br><span class="line">      &#123; msg: &#x27;Kung Fury&#x27; &#125;</span><br><span class="line">    ]</span><br><span class="line">  &#125;,</span><br><span class="line">  computed: &#123;</span><br><span class="line">    computedList: function () &#123;</span><br><span class="line">      var vm = this</span><br><span class="line">      return this.list.filter(function (item) &#123;</span><br><span class="line">        return item.msg.toLowerCase().indexOf(vm.query.toLowerCase()) !== -1</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">    beforeEnter: function (el) &#123;</span><br><span class="line">      el.style.opacity = 0</span><br><span class="line">      el.style.height = 0</span><br><span class="line">    &#125;,</span><br><span class="line">    enter: function (el, done) &#123;</span><br><span class="line">      var delay = el.dataset.index * 150</span><br><span class="line">      setTimeout(function () &#123;</span><br><span class="line">        Velocity(</span><br><span class="line">          el,</span><br><span class="line">          &#123; opacity: 1, height: &#x27;1.6em&#x27; &#125;,</span><br><span class="line">          &#123; complete: done &#125;</span><br><span class="line">        )</span><br><span class="line">      &#125;, delay)</span><br><span class="line">    &#125;,</span><br><span class="line">    leave: function (el, done) &#123;</span><br><span class="line">      var delay = el.dataset.index * 150</span><br><span class="line">      setTimeout(function () &#123;</span><br><span class="line">        Velocity(</span><br><span class="line">          el,</span><br><span class="line">          &#123; opacity: 0, height: 0 &#125;,</span><br><span class="line">          &#123; complete: done &#125;</span><br><span class="line">        )</span><br><span class="line">      &#125;, delay)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>



<h3 id="可复用的过渡"><a href="#可复用的过渡" class="headerlink" title="可复用的过渡"></a>可复用的过渡</h3><p>​        可以使用Vue组件系统的实现复用。只需要将 transition 或者 transition-group 作为根组件。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">Vue.component(&#x27;my-special-transition&#x27;, &#123;</span><br><span class="line">  template: &#x27;\</span><br><span class="line">    &lt;transition\</span><br><span class="line">      name=&quot;very-special-transition&quot;\</span><br><span class="line">      mode=&quot;out-in&quot;\</span><br><span class="line">      v-on:before-enter=&quot;beforeEnter&quot;\</span><br><span class="line">      v-on:after-enter=&quot;afterEnter&quot;\</span><br><span class="line">    &gt;\</span><br><span class="line">      &lt;slot&gt;&lt;/slot&gt;\</span><br><span class="line">    &lt;/transition&gt;\</span><br><span class="line">  &#x27;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">    beforeEnter: function (el) &#123;</span><br><span class="line">      // ...</span><br><span class="line">    &#125;,</span><br><span class="line">    afterEnter: function (el) &#123;</span><br><span class="line">      // ...</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>​    当然，可以使用函数式组件来完成这个任务。函数式组件我们后面在进行了解。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">Vue.component(&#x27;my-special-transition&#x27;, &#123;</span><br><span class="line">  functional: true,</span><br><span class="line">  render: function (createElement, context) &#123;</span><br><span class="line">    var data = &#123;</span><br><span class="line">      props: &#123;</span><br><span class="line">        name: &#x27;very-special-transition&#x27;,</span><br><span class="line">        mode: &#x27;out-in&#x27;</span><br><span class="line">      &#125;,</span><br><span class="line">      on: &#123;</span><br><span class="line">        beforeEnter: function (el) &#123;</span><br><span class="line">          // ...</span><br><span class="line">        &#125;,</span><br><span class="line">        afterEnter: function (el) &#123;</span><br><span class="line">          // ...</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    return createElement(&#x27;transition&#x27;, data, context.children)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>



<h3 id="动态过渡"><a href="#动态过渡" class="headerlink" title="动态过渡"></a>动态过渡</h3><p>​        在Vue中，过渡也是数据驱动的。所以name也可以使用 v-bind 来进行绑定。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;transition v-bind:name=&quot;transitionName&quot;&gt;</span><br></pre></td></tr></table></figure>

<p>​        所有的过渡 attribute 都可以动态绑定。因为事件钩子都是方法。所以根据组件的状态不同，过渡也会有不同的表现。</p>
<h2 id="状态过渡"><a href="#状态过渡" class="headerlink" title="状态过渡"></a>状态过渡</h2><p>​        数据元素本身的特效，这些数据要么本身就以数值形式存储，要么可以转换为数值。</p>
<ul>
<li>数字和运算</li>
<li>颜色的显示</li>
<li>SVG 节点的位置</li>
<li>元素的大小和其他的 property</li>
</ul>
<h3 id="状态动画与侦听器"><a href="#状态动画与侦听器" class="headerlink" title="状态动画与侦听器"></a>状态动画与侦听器</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">&lt;script src=&quot;https://cdn.jsdelivr.net/npm/tween.js@16.3.4&quot;&gt;&lt;/script&gt;</span><br><span class="line">&lt;script src=&quot;https://cdn.jsdelivr.net/npm/color-js@1.0.3&quot;&gt;&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">&lt;div id=&quot;example-7&quot;&gt;</span><br><span class="line">  &lt;input</span><br><span class="line">    v-model=&quot;colorQuery&quot;</span><br><span class="line">    v-on:keyup.enter=&quot;updateColor&quot;</span><br><span class="line">    placeholder=&quot;Enter a color&quot;</span><br><span class="line">  &gt;</span><br><span class="line">  &lt;button v-on:click=&quot;updateColor&quot;&gt;Update&lt;/button&gt;</span><br><span class="line">  &lt;p&gt;Preview:&lt;/p&gt;</span><br><span class="line">  &lt;span</span><br><span class="line">    v-bind:style=&quot;&#123; backgroundColor: tweenedCSSColor &#125;&quot;</span><br><span class="line">    class=&quot;example-7-color-preview&quot;</span><br><span class="line">  &gt;&lt;/span&gt;</span><br><span class="line">  &lt;p&gt;&#123;&#123; tweenedCSSColor &#125;&#125;&lt;/p&gt;</span><br><span class="line">&lt;/div&gt;</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line">var Color = net.brehaut.Color</span><br><span class="line"></span><br><span class="line">new Vue(&#123;</span><br><span class="line">  el: &#x27;#example-7&#x27;,</span><br><span class="line">  data: &#123;</span><br><span class="line">    colorQuery: &#x27;&#x27;,</span><br><span class="line">    color: &#123;</span><br><span class="line">      red: 0,</span><br><span class="line">      green: 0,</span><br><span class="line">      blue: 0,</span><br><span class="line">      alpha: 1</span><br><span class="line">    &#125;,</span><br><span class="line">    tweenedColor: &#123;&#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  created: function () &#123;</span><br><span class="line">    this.tweenedColor = Object.assign(&#123;&#125;, this.color)</span><br><span class="line">  &#125;,</span><br><span class="line">  watch: &#123;</span><br><span class="line">    color: function () &#123;</span><br><span class="line">      function animate () &#123;</span><br><span class="line">        if (TWEEN.update()) &#123;</span><br><span class="line">          requestAnimationFrame(animate)</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      new TWEEN.Tween(this.tweenedColor)</span><br><span class="line">        .to(this.color, 750)</span><br><span class="line">        .start()</span><br><span class="line"></span><br><span class="line">      animate()</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  computed: &#123;</span><br><span class="line">    tweenedCSSColor: function () &#123;</span><br><span class="line">      return new Color(&#123;</span><br><span class="line">        red: this.tweenedColor.red,</span><br><span class="line">        green: this.tweenedColor.green,</span><br><span class="line">        blue: this.tweenedColor.blue,</span><br><span class="line">        alpha: this.tweenedColor.alpha</span><br><span class="line">      &#125;).toCSS()</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">    updateColor: function () &#123;</span><br><span class="line">      this.color = new Color(this.colorQuery).toRGB()</span><br><span class="line">      this.colorQuery = &#x27;&#x27;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">.example-7-color-preview &#123;</span><br><span class="line">  display: inline-block;</span><br><span class="line">  width: 50px;</span><br><span class="line">  height: 50px;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="动态状态过渡"><a href="#动态状态过渡" class="headerlink" title="动态状态过渡"></a>动态状态过渡</h3><p>​        就像 Vue 的过渡组件一样，数据背后状态过渡会实时更新，这对于原型设计十分有用。当你修改一些变量，即使是一个简单的 SVG 多边形也可实现很多难以想象的效果。</p>
<h3 id="把过渡放到组件里"><a href="#把过渡放到组件里" class="headerlink" title="把过渡放到组件里"></a>把过渡放到组件里</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line">&lt;script src=&quot;https://cdn.jsdelivr.net/npm/tween.js@16.3.4&quot;&gt;&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">&lt;div id=&quot;example-8&quot;&gt;</span><br><span class="line">  &lt;input v-model.number=&quot;firstNumber&quot; type=&quot;number&quot; step=&quot;20&quot;&gt; +</span><br><span class="line">  &lt;input v-model.number=&quot;secondNumber&quot; type=&quot;number&quot; step=&quot;20&quot;&gt; =</span><br><span class="line">  &#123;&#123; result &#125;&#125;</span><br><span class="line">  &lt;p&gt;</span><br><span class="line">    &lt;animated-integer v-bind:value=&quot;firstNumber&quot;&gt;&lt;/animated-integer&gt; +</span><br><span class="line">    &lt;animated-integer v-bind:value=&quot;secondNumber&quot;&gt;&lt;/animated-integer&gt; =</span><br><span class="line">    &lt;animated-integer v-bind:value=&quot;result&quot;&gt;&lt;/animated-integer&gt;</span><br><span class="line">  &lt;/p&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line">// 这种复杂的补间动画逻辑可以被复用</span><br><span class="line">// 任何整数都可以执行动画</span><br><span class="line">// 组件化使我们的界面十分清晰</span><br><span class="line">// 可以支持更多更复杂的动态过渡</span><br><span class="line">// 策略。</span><br><span class="line">Vue.component(&#x27;animated-integer&#x27;, &#123;</span><br><span class="line">  template: &#x27;&lt;span&gt;&#123;&#123; tweeningValue &#125;&#125;&lt;/span&gt;&#x27;,</span><br><span class="line">  props: &#123;</span><br><span class="line">    value: &#123;</span><br><span class="line">      type: Number,</span><br><span class="line">      required: true</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  data: function () &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      tweeningValue: 0</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  watch: &#123;</span><br><span class="line">    value: function (newValue, oldValue) &#123;</span><br><span class="line">      this.tween(oldValue, newValue)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  mounted: function () &#123;</span><br><span class="line">    this.tween(0, this.value)</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">    tween: function (startValue, endValue) &#123;</span><br><span class="line">      var vm = this</span><br><span class="line">      function animate () &#123;</span><br><span class="line">        if (TWEEN.update()) &#123;</span><br><span class="line">          requestAnimationFrame(animate)</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      new TWEEN.Tween(&#123; tweeningValue: startValue &#125;)</span><br><span class="line">        .to(&#123; tweeningValue: endValue &#125;, 500)</span><br><span class="line">        .onUpdate(function () &#123;</span><br><span class="line">          vm.tweeningValue = this.tweeningValue.toFixed(0)</span><br><span class="line">        &#125;)</span><br><span class="line">        .start()</span><br><span class="line"></span><br><span class="line">      animate()</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">// 所有的复杂度都已经从 Vue 的主实例中移除！</span><br><span class="line">new Vue(&#123;</span><br><span class="line">  el: &#x27;#example-8&#x27;,</span><br><span class="line">  data: &#123;</span><br><span class="line">    firstNumber: 20,</span><br><span class="line">    secondNumber: 40</span><br><span class="line">  &#125;,</span><br><span class="line">  computed: &#123;</span><br><span class="line">    result: function () &#123;</span><br><span class="line">      return this.firstNumber + this.secondNumber</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>



<h3 id="赋予设计以生命"><a href="#赋予设计以生命" class="headerlink" title="赋予设计以生命"></a>赋予设计以生命</h3>
    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Vue/" rel="tag"># Vue</a>
              <a href="/tags/%E6%96%87%E6%A1%A3/" rel="tag"># 文档</a>
              <a href="/tags/Vue2/" rel="tag"># Vue2</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/10/29/Vue/Vue2%E6%95%99%E7%A8%8B/Vue2-%E6%95%99%E7%A8%8B-%E6%B7%B1%E5%85%A5%E4%BA%86%E8%A7%A3%E7%BB%84%E4%BB%B6/" rel="prev" title="Vue-使用-深入了解组件">
                  <i class="fa fa-chevron-left"></i> Vue-使用-深入了解组件
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/10/29/Vue/Vue2-%E6%95%99%E7%A8%8B/Vue2-%E6%95%99%E7%A8%8B-%E5%B7%A5%E5%85%B7/" rel="next" title="Vue-使用-工具">
                  Vue-使用-工具 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">tallgy</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
